/*
 * Useful URLs
 * http://www.codeproject.com/KB/security/CryptoInteropSign.aspx
 * http://www.java2s.com/Code/VB/Development/GenerateDSAKeyandvalue.htm
 * http://www.codeproject.com/KB/security/porting_java_public_key.aspx
 * http://rigvinath.blogspot.com/2008/06/dsa-signature-conversion-java-to-net.html
 * http://www.visualbuilder.com/showCode.php?id=79585&scd_id=1534
 * http://software.topcoder.com/catalog/document?id=26841391
 */

using System;
using System.Security.Cryptography;

namespace IllSoft.Xvcc.Volano.Client
{
    class KeyGen
    {
        private static DSAParameters DSAKeyInfo;
        private static DSACryptoServiceProvider DSA;

        /// <summary>
        /// (De-)Constructor(s)
        /// </summary>
        public KeyGen()
        {
            DSAKeyInfo = new DSAParameters();
            DSA = new DSACryptoServiceProvider();
        }

        public byte[] GenerateSignedKey(byte[] inputKey)
        {
            this.ImportParameters();
            return ToJavaDSA(DSA.SignData(inputKey));
        }

        private void ImportParameters()
        {
            try
            {
                // v2.6.2.1
                DSAKeyInfo.Y = Convert.FromBase64String("V0VFGqmkb+NOxyBj5GI4BaStG7MhFVEh3Q/hMS2i0mBTLlMjhVp1Ya81okR2+YrTiszkV640pvaM\r\r\ndWjU9olK8A==");
                DSAKeyInfo.X = Convert.FromBase64String("QfGmYcS2qYxk/5qgq5MzbmqdX88=");
                DSAKeyInfo.G = Convert.FromBase64String("Z4Rxsnqc9E7pGknFFH2xqaryRPBaQ01khpMdLRQnG541Awtx/XPaF5Bpsy4pNWMOHCBiNU0Nogps\r\r\nQW5QvnlMpA==");
                DSAKeyInfo.P = Convert.FromBase64String("/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxeEu0ImbzRMqzVDZkVG9\r\r\nxD7nN1kuFw==");
                DSAKeyInfo.Q = Convert.FromBase64String("li7dzDacuo67Jg7mtqEm2TRuOMU=");

                // v2.7.1
                //DSAKeyInfo.Y = Convert.FromBase64String("4zcXrOZmXvUWgu/qrLKRoMh/l69rLlpvLDH1Ta6I9f27u4LhW1iFTlvFaK0EpV4ZOCDXZuNrBh4A\r\r\nrsMbinzKMg==");
                //DSAKeyInfo.X = Convert.FromBase64String("EBfJBiVtwUdIYSzTpAxKvYAIWzo=");
                //DSAKeyInfo.G = Convert.FromBase64String("Z4Rxsnqc9E7pGknFFH2xqaryRPBaQ01khpMdLRQnG541Awtx/XPaF5Bpsy4pNWMOHCBiNU0Nogps\r\r\nQW5QvnlMpA==");
                //DSAKeyInfo.P = Convert.FromBase64String("/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxeEu0ImbzRMqzVDZkVG9\r\r\nxD7nN1kuFw==");
                //DSAKeyInfo.Q = Convert.FromBase64String("li7dzDacuo67Jg7mtqEm2TRuOMU=");
                DSA.ImportParameters(DSAKeyInfo);
            }
            catch (Exception ex)
            {
                throw new Exception("Import_Error: " + ex);
            }
        }

        // These values have to be added because of the different signature formats used
        // DER (Java) vs. IEEE P1363 (C#)
        private byte[] ToJavaDSA(byte[] signedKey)
        {
            byte[] javaDSAKey = new byte[signedKey.Length + 8];

            javaDSAKey[0] = 48;
            javaDSAKey[1] = 46;
            javaDSAKey[2] = 2;
            javaDSAKey[3] = 21;
            javaDSAKey[4] = 0;
            javaDSAKey[25] = 2;
            javaDSAKey[26] = 21;
            javaDSAKey[27] = 0;

            for (int i = 0; i < 20; i++)
            {
                javaDSAKey[i + 5] = signedKey[i];
            }

            for (int i = 20; i < 40; i++)
            {
                javaDSAKey[i + 8] = signedKey[i];
            }

            return javaDSAKey;
        }
    }
}